﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Noea.HTTP;
using Noea.Graph;
using Noea.Parser;
using System.Collections;
using System.Windows.Forms;


namespace WebCrawler
{
    public class Crawler
    {
        private BasicGraph sitemap;
        private HTTPTCPClient socket;
        private Vertex root;
		private string fullAddress;

        public Crawler(string url)
        {
            socket = new HTTPTCPClient();
            sitemap = new BasicGraph();
            fullAddress = url.Substring(0, url.LastIndexOf("/") + 1);
            root = new Vertex(url.Substring(url.LastIndexOf("/") + 1));
        }

        public BasicGraph Sitemap
        {
            get { return sitemap; }
        }

        public string URL
        {
            get { return root.Name; }
        }

        public void BuildSiteMap()
        {
            Queue<Vertex> queue = new Queue<Vertex>();
            sitemap.AddVertex(root);
            queue.Enqueue(root);
            while (queue.Count > 0)
            {
                Vertex vertex = queue.Dequeue();
				socket.Open(fullAddress + vertex.Name, 80);
                socket.SendRequest();
                string result = socket.Receive();
                IHTMLDoc parser = new HTMLDocRegEx(result);
                IEnumerator urls = parser.GetAllLinks().GetEnumerator();
                while (urls.MoveNext())
                {
                    string url = (string)urls.Current;
                    if (url.Contains("#")) url = url.Substring(0, url.IndexOf("#"));
                    if (url.Length == 0) continue;
                    Vertex v = sitemap.GetVertexByName((string)urls.Current);
                    if (vertex == v) continue;
                    if (v == null)
                    {
                        v = new Vertex((string)urls.Current);
                        sitemap.AddVertex(v);
                        vertex.AddEdgeTo(v);
                        queue.Enqueue(v);
                    }
                    else
                    {
                        vertex.AddEdgeTo(v);
                    }
                }
            }
        }

        public Vertex GetNextUnvisited(IList links)
        {
           
            throw new NotImplementedException();
        }

        public IEnumerator TraverseMap()
        {
            throw new NotImplementedException();
        }

    }
}